home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / OutOfContextMenus / Source / TerminationUtils.cp < prev    next >
Encoding:
Text File  |  1999-06-21  |  2.8 KB  |  132 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    TerminationUtils.cp                     ©1999 Eric Traut
  3. // ===========================================================================
  4.  
  5. #include "TerminationUtils.h"
  6.  
  7. #ifndef __MIXEDMODE__
  8. #include <MixedMode.h>
  9. #endif
  10.  
  11. #ifndef __TRAPS__
  12. #include <Traps.h>
  13. #endif
  14.  
  15. #ifndef __OSUTILS__
  16. #include <OSUtils.h>
  17. #endif
  18.  
  19. /* Global and static variables */
  20. static TearDownProcPtr    sTearDownProcs[kMaxTearDownProcs];
  21. static Boolean             sDisableTearDownProcs = false;
  22.  
  23. static void                ExecuteTearDownProcs(void);
  24.  
  25. extern "C"
  26. {
  27. void    TerminationProc(void);
  28. }
  29.  
  30.  
  31. /*------------------------------------------------------------------
  32.     InitTerminationUtils
  33.     
  34.     This routine initializes the module.
  35. ------------------------------------------------------------------*/
  36.  
  37. void
  38. InitTerminationUtils(void)
  39. {
  40.     UInt32                            curProcIndex;
  41.  
  42.     // Initialize tear-down proc array
  43.     for (curProcIndex = 0; curProcIndex < kMaxTearDownProcs; curProcIndex++)
  44.         sTearDownProcs[curProcIndex] = NULL;
  45. }
  46.  
  47.  
  48. /*------------------------------------------------------------------
  49.     DisableTearDownProcs
  50.     
  51.     This routine tells the termination module that the tear-down
  52.     procs are unnecessary because normal termination has occured.
  53. ------------------------------------------------------------------*/
  54.  
  55. void
  56. DisableTearDownProcs(void)
  57. {
  58.     sDisableTearDownProcs = true;
  59. }
  60.  
  61.  
  62. /*------------------------------------------------------------------
  63.     TerminationProc
  64.     
  65.     This routine is the CFM fragment's termination proc.
  66. ------------------------------------------------------------------*/
  67.  
  68. void
  69. TerminationProc(void)
  70. {
  71.     ExecuteTearDownProcs();
  72. }
  73.  
  74.  
  75. /*------------------------------------------------------------------
  76.     ExecuteTearDownProcs
  77.     
  78.     This routine executes all the currently installed tear-down
  79.     procs.
  80. ------------------------------------------------------------------*/
  81.  
  82. void
  83. ExecuteTearDownProcs(void)
  84. {
  85.     SInt32                        curProcIndex;
  86.  
  87.     if (!sDisableTearDownProcs)
  88.     {
  89.         sDisableTearDownProcs = true;
  90.  
  91.         // Find first empty entry in array
  92.         for (curProcIndex = kMaxTearDownProcs - 1; curProcIndex >= 0; curProcIndex--)
  93.         {
  94.             TearDownProcPtr            curProc;
  95.             
  96.             curProc = sTearDownProcs[curProcIndex];
  97.                 
  98.             // Call the current proc
  99.             if (curProc != NULL)
  100.                 (*curProc)();
  101.         }
  102.     }
  103. }
  104.  
  105.  
  106. /*------------------------------------------------------------------
  107.     InstallTearDownProc
  108.     
  109.     Parameters:
  110.         In:        inProcToCall    - native procedure to call on termination
  111. ------------------------------------------------------------------*/
  112.  
  113. void
  114. InstallTearDownProc(TearDownProcPtr inProcToCall)
  115. {
  116.     UInt32                            curProcIndex;
  117.     
  118.     // Find first empty entry in array
  119.     for (curProcIndex = 0; curProcIndex < kMaxTearDownProcs; curProcIndex++)
  120.     {
  121.         if (sTearDownProcs[curProcIndex] == NULL)
  122.             break;
  123.     }
  124.     
  125.     if (curProcIndex < kMaxTearDownProcs)
  126.         sTearDownProcs[curProcIndex] = inProcToCall;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.